<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>建立一个包含指定范围单元的数组</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.prev.html">prev</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.reset.html">reset</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.range" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">range</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">range</span> &mdash; <span class="dc-title">
     建立一个包含指定范围单元的数组
    </span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.range-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>range</strong></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$start</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$limit</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.number" class="type number">number</a></span> <code class="parameter">$step</code><span class="initializer"> = 1</span></span>
  ] )</div>

    <p class="para rdfs-comment">
   建立一个包含指定范围单元的数组。
  </p>
 </div>

   
  <div class="refsect1 parameters" id="refsect1-function.range-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">start</code></em></dt>

     <dd>

      <p class="para">
       序列的第一个值。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">limit</code></em></dt>

     <dd>

      <p class="para">
       序列结束于 <em><code class="parameter">limit</code></em> 的值。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">step</code></em></dt>

     <dd>

      <p class="para">
       如果给出了 <em><code class="parameter">step</code></em> 的值，它将被作为单元之间的步进值。<em><code class="parameter">step</code></em>
     应该为正值。如果未指定，<em><code class="parameter">step</code></em> 则默认为 1。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.range-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回的数组中从
     <em><code class="parameter">start</code></em> 到 <em><code class="parameter">limit</code></em> 的单元，包括它们本身。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.range-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.0.0</td>
       <td>
        添加了可选参数  <em><code class="parameter">step</code></em>。
       </td>
      </tr>

      <tr>
       <td>4.1.0 to 4.3.2</td>
       <td>
        在 PHP 版本 4.1.0 到 4.3.2 中，<span class="function"><strong>range()</strong></span>
      将数字字符串看作字符串而不是整数，因此将会被作为字符序列使用。例如，<em>&quot;4242&quot;</em>
      会被当作 <em>&quot;4&quot;</em> 来对待。
       </td>
      </tr>

      <tr>
       <td>4.1.0</td>
       <td>
        在版本 4.1.0 之前 <span class="function"><strong>range()</strong></span>
      函数只产生递增的整数数组。对于字符序列和递减数组的支持是 4.1.0
      加入的。字符序列值的长度限定为一。如果输入的长度超过一，则只使用第一个字符。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.range-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4929">
    <p><strong>Example #1 <span class="function"><strong>range()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;array(0,&nbsp;1,&nbsp;2,&nbsp;3,&nbsp;4,&nbsp;5,&nbsp;6,&nbsp;7,&nbsp;8,&nbsp;9,&nbsp;10,&nbsp;11,&nbsp;12)<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">12</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;step&nbsp;parameter&nbsp;was&nbsp;introduced&nbsp;in&nbsp;5.0.0<br />//&nbsp;array(0,&nbsp;10,&nbsp;20,&nbsp;30,&nbsp;40,&nbsp;50,&nbsp;60,&nbsp;70,&nbsp;80,&nbsp;90,&nbsp;100)<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$number</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Use&nbsp;of&nbsp;character&nbsp;sequences&nbsp;introduced&nbsp;in&nbsp;4.1.0<br />//&nbsp;array('a',&nbsp;'b',&nbsp;'c',&nbsp;'d',&nbsp;'e',&nbsp;'f',&nbsp;'g',&nbsp;'h',&nbsp;'i');<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'i'</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$letter</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$letter</span><span style="color: #007700">;<br />}<br /></span><span style="color: #FF8000">//&nbsp;array('c',&nbsp;'b',&nbsp;'a');<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #DD0000">'c'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'a'</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$letter</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$letter</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.range-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.shuffle.html" class="function" rel="rdfs-seeAlso">shuffle()</a> - 将数组打乱</span></li>
    <li class="member"><span class="function"><a href="function.array-fill.html" class="function" rel="rdfs-seeAlso">array_fill()</a> - 用给定的值填充数组</span></li>
    <li class="member"><a href="control-structures.foreach.html" class="link">foreach</a></li>
   </ul>
  </p>
 </div>
  
   
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.prev.html">prev</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.reset.html">reset</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
